package com.yunhe.merchants.controller;

import com.yunhe.common.annotation.OperationRecord;
import com.yunhe.common.constant.SystemConsts;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.common.utils.ExcelUtils;
import com.yunhe.merchants.domain.DataProject;
import com.yunhe.merchants.model.DataProjectCondition;
import com.yunhe.merchants.model.DataProjectUpload;
import com.yunhe.merchants.util.BeansUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.yunhe.common.util.CollectionUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.yunhe.merchants.service.IDataProjectService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 经理项目池与公共商机库
 * @author hetao
 * @date 2021/05/10
 */
@Api(value = "dataProject", description = "经理项目池与公共商机库维护", tags = {"Model_DATA_PROJECT"})
@RestController
@RequestMapping(value = "/projects")
public class DataProjectController {

    @Autowired
    private IDataProjectService dataProjectService;

    /**
     * 刪除
     * @author hetao
     * @date 2021/05/10
     **/
    @ApiOperation("通过id批量删除项目信息")
    @RequestMapping(value = "/deleteDataProject",method = RequestMethod.POST)
    public ResponseEntity<ResultObject> delete(@RequestBody List<String> ids) throws ArgumentErrorException {
        dataProjectService.deleteDataProject(ids);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    @ApiOperation("通过条件查询项目池信息")
    @RequestMapping(value = "/findProjectByCondition",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> findProjectByCondition(@ModelAttribute DataProjectCondition dataProjectCondition) throws ArgumentErrorException {
        PageResult pageResult = dataProjectService.findProjectByCondition(dataProjectCondition);
        return new ResponseEntity<>(new ResultObject<>(pageResult), HttpStatus.OK);
    }

    @ApiOperation("新增项目池信息")
    @RequestMapping(method = RequestMethod.POST)
    @OperationRecord(menu = "经理项目池与公共商机库维护", actionType = SystemConsts.ACTION.ADD)
    public ResponseEntity<ResultObject> createDataProject(@RequestBody DataProject dataProject) throws ArgumentErrorException {
        DataProject newDataProject = dataProjectService.createDataProject(dataProject);
        return new ResponseEntity<>(new ResultObject<>(newDataProject), HttpStatus.OK);
    }

    @ApiOperation("通过id查看项目池信息")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getDataPorjectById(@PathVariable("id") String id) throws ArgumentErrorException {
        DataProject newDataProject = dataProjectService.getDataPorjectById(id);
        return new ResponseEntity<>(new ResultObject<>(newDataProject), HttpStatus.OK);
    }

    @ApiOperation("通过企业id分页查看历史状态信息")
    @RequestMapping(value = "/getHistoryStatus/{id}",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getHistoryStatus(@PathVariable("id") String id,
                                                         @RequestParam(value = "page",required = true) int page,
                                                         @RequestParam(value = "size") int size) throws ArgumentErrorException {
        PageResult pageResult = dataProjectService.getHistoryStatus(id,page,size);
        return new ResponseEntity<>(new ResultObject<>(pageResult), HttpStatus.OK);
    }

    @ApiOperation("招商经理将项目信息分配给对应的业务员")
    @RequestMapping(value = "/allocationProjects",method = RequestMethod.POST)
    public ResponseEntity<ResultObject> allocationProjects(@RequestParam(value = "ids",required = true) String ids,
                                                           @RequestParam(value = "salesman",required = true) Long salesman,
                                                           @RequestParam(value = "deptId",required = true) Long deptId) throws ArgumentErrorException {
        dataProjectService.allocationProjects(ids,salesman,deptId);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    @ApiOperation("将经理项目池的信息公开到公共商机库")
    @RequestMapping(value = "/overt",method = RequestMethod.POST)
    public ResponseEntity<ResultObject> overt(@RequestParam(value = "ids",required = true) String ids) throws ArgumentErrorException {
        dataProjectService.overt(ids);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    @ApiOperation("将经理项目池的数据移个人的历史项目并从经理项目池移出")
    @RequestMapping(value = "/moveOut/{id}",method = RequestMethod.POST)
    public ResponseEntity<ResultObject> moveOut(@PathVariable(value = "id") String id) throws ArgumentErrorException {
        dataProjectService.moveOut(id);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    @ApiOperation("业务员在公共商机库进行认领")
    @RequestMapping(value = "/claim",method = RequestMethod.POST)
    public ResponseEntity<ResultObject> claim(@RequestParam(value = "ids") String ids) throws ArgumentErrorException {
        dataProjectService.claim(ids);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    @ApiOperation("经理项目池批量导入")
    @RequestMapping(value = "/uploadManageProject",method = RequestMethod.POST)
    public ResponseEntity<ResultObject> uploadManagePorejct(@RequestBody MultipartFile file) throws ArgumentErrorException, IOException {
       dataProjectService.uploadManageProject(file);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    @ApiOperation("经理项目池/公共商机库导出")
    @RequestMapping(value = "/exportManageProject",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> exportManageProject(@RequestParam(value = "ids",required = false) String ids,
                                                            @ModelAttribute DataProjectCondition dataProjectCondition,
                                                            HttpServletResponse httpServletResponse) throws ArgumentErrorException, IOException {
        List<DataProject> dataProjects = dataProjectService.exportManageProject(ids,dataProjectCondition);
        if(CollectionUtil.isNotEmpty(dataProjects)){
            List<DataProjectUpload> dataProjectUploads = BeansUtil.listCopy(dataProjects,DataProjectUpload.class);
            if(CollectionUtil.isNotEmpty(dataProjectUploads)){
                ExcelUtils.export(dataProjectUploads,httpServletResponse, DataProjectUpload.class);
            }
        }
        return null;
    }

    @ApiOperation("公共商机库批量导入")
    @RequestMapping(value = "/uploadPublicProject",method = RequestMethod.POST)
    public ResponseEntity<ResultObject> uploadPublicPorejct(@RequestBody MultipartFile file) throws ArgumentErrorException, IOException {
        dataProjectService.uploadPublicProject(file);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }


}
